package com.cheejee.goodscenter.controller;

import static com.cheejee.goodscenter.module.response.BaseReponse.addOk;
import static com.cheejee.goodscenter.module.response.BaseReponse.changeOk;
import static com.cheejee.goodscenter.module.response.BaseReponse.deleteOk;
import static com.cheejee.goodscenter.module.response.BaseReponse.queryOk;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cheejee.goodscenter.module.dto.SkuDto;
import com.cheejee.goodscenter.service.SkuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author CARRY
 * @Date 2020年11月2日下午3:15:10
 */
@RestController
@RequestMapping("/sku")
@Api(tags = "SKU")
public class SkuController {

	@Resource
	private SkuService service;
	
	
	@GetMapping("/")
	@ApiOperation(value = "按条件查询SKU信息", notes = "多个条件AND连接，如果参数都为null则查询全部的SKU")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "goodsId", value = "商品ID", dataType = "long"),
		@ApiImplicitParam(name = "isOn", value = "是否上架", dataType = "boolean"),
		@ApiImplicitParam(name = "pageSize", value = "每页内容数", defaultValue = "0", dataType = "int"),
		@ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "0", dataType = "int") })
	public Object getSkuInfoByGoods(Long goodsId, boolean isOn, Integer pageSize, Integer pageNum) {
		
		return queryOk(service.querySkuDto(goodsId, null, null, 0, 0).getList());
	}
	
	@GetMapping("/{id}")
	@ApiOperation(value = "查询某SKU的信息", notes = "包含SKU的属性信息，规格信息重新包装")
	@ApiImplicitParam(name = "id", value = "SKU ID")
	public Object getSkuInfo(@PathVariable Long id) {
		
		return queryOk(service.querySkuInfoById(id));
	}
	
	@PutMapping("/")
	@ApiOperation(value = "新增SKU", notes = "如果SKU携带有属性信息，则会新增SKU属性信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "goodsId", value = "商品ID"),
		@ApiImplicitParam(name = "sku", value = "添加的SKU列表",allowMultiple = true, dataType = "SkuDto")
	})
	public Object addSku(Long goodsId, @RequestBody List<SkuDto> sku) {
		service.addSku(goodsId, sku);
		
		return addOk(sku);
	}
	
	@PutMapping("/change")
	@ApiOperation(value = "修改SKU信息", notes = "一同修改SKU的属性")
	public Object changeSkuByGoods(Long goodsId, @RequestBody SkuDto sku) {
		service.changeSkuinfo(goodsId, sku);
		
		return changeOk(service.querySkuInfoById(sku.getId()));
	}
	
	@DeleteMapping("/{id}")
	@ApiOperation(value = "删除SKU")
	public Object deleteSku(Long[] id) {
		service.deleteSku(id);
		
		return deleteOk();
	}
	
}
